<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <span v-if="isEdit">
        <span @click="searchHistory.splice(0)">全部删除</span>&nbsp;&nbsp;
        <span @click="isEdit=false">完成</span>
      </span>
      <van-icon v-else name="delete" @click="isEdit=true" />
    </van-cell>
    <van-cell 
      v-for="(item,index) in searchHistory" 
      :key="index" 
      @click="historyClick(item,index)"
      :title="item">
      <van-icon v-if="isEdit" name="close" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  components: {},
  props: {
    searchHistory:{
      type:Array
    }
  },
  data () {
    return {
      isEdit:false  // 是否处于编辑状态
    }
  },
  methods: {
    // 历史记录点击行为
    historyClick(val,idx){
      // 判断是否处于编辑状态
      if(this.isEdit){
        // 是：删除操作
        this.searchHistory.splice(idx,1)
      }else{
        // 否：将历史记录项放到输入框，显示搜索结果
        this.$emit('search',val)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
